﻿using System;
using System.Windows.Forms;

namespace AmazonBookLinker
{
  public partial class TemplateTokensFormView : Form, ITemplateTokensView
  {
    private TemplateTokensViewPresenter presenter;

    #region Constructors

    public TemplateTokensFormView()
    {
      InitializeComponent();
      HookupFormEvents();
      InitializePresenter();
    }

    #endregion

    #region Methods

    private void InitializePresenter()
    {
      this.presenter = new TemplateTokensViewPresenter( this, new TemplateTokensModel() );
      HookupViewEvents();
    }

    private void HookupViewEvents()
    {
      this.ClosingView += this.presenter.OnClosingView;
    }

    private void HookupFormEvents()
    {
      this.Load += OnLoad;
      this.CloseButton.Click += OnCloseButton_Click;
    }

    private void OnCloseButton_Click( object sender, EventArgs e )
    {
      var handler = this.ClosingView;
      if ( handler != null )
      {
        handler( sender, e );
      }
    }

    private bool loaded = false;
    private void OnLoad( object sender, EventArgs e )
    {
      if (!loaded)
      {
        loaded = true;
        base.OnLoad(e);
      }
      this.presenter.Initialize();
    }

    public void CloseView()
    {
      this.DialogResult = DialogResult.OK;
    }

    #endregion


    #region Events

    public event EventHandler ClosingView;

    #endregion

    #region Properties

    public string Content
    {
      get { return this.txtContent.Text; }

      set { this.txtContent.Text = value; }
    }

    #endregion
  }
}